package com.demo.springcloud.controller;

import com.demo.springcloud.entity.Payment;
import com.demo.springcloud.entity.R;
import com.demo.springcloud.service.IPaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author hxg
 * @version 1.0
 * @date 2021/12/23 18:10
 */
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
    @Resource
    private IPaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/save")
    public R save(@RequestBody Payment payment){
        boolean save = paymentService.save(payment);
        if (save) {
            return new R(200, "success"+serverPort);
        }
        return new R(400,"false"+serverPort);
    }

    @GetMapping("/get/{id}")
    public R get(@PathVariable("id")Long id){
        Payment payment = paymentService.getOrder(id);
        if (payment != null) {
            return new R(200, "success"+serverPort,payment+"asd");
        }
        return new R(400,"false"+serverPort);
    }

    @GetMapping("/remove/{id}")
    public R remove(@PathVariable("id")Long id){
        boolean b = paymentService.removeById(id);
        if (b) {
            return new R(200, "success"+serverPort);
        }
        return new R(400,"false"+serverPort);
    }
    @GetMapping("/timeoutTest")
    public R timeoutTest(){
        try {
            Thread.sleep(3000);
            return new R(400,"true"+serverPort);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new R(400,"false"+serverPort);
    }

    @GetMapping("/zipkin")
    public R paymentZipkin(){
        return new R(200,"zipkin start.");
    }

}
